﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace myTravian
{
    public partial class traBuildUp : Form
    {
        public traBuildUp()
        {
            InitializeComponent();
        }

        private void btnAddin_Click(object sender, EventArgs e)
        {
            Addin();
        }

        /// <summary>
        /// 把目前停留的那個建物捉下來
        /// </summary>
        void Addin()
        {
            string strBuilding,strURL;
            HtmlElementCollection buildings ;
            strBuilding = "";
            buildings = traBrowser1.GetHtmlDocument().GetElementsByTagName("h1");
            strURL = traBrowser1.GetCurrentURL();
            strBuilding = buildings[0].InnerText; // //<h1>農田 <span class="level">等級 7</span></h1>

            listBox1.Items.Add(new traBuilding(strURL, strBuilding));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            traBrowser1.NavigateURL(this.txt_url.Text);
        }

        private void btnClearAll_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if(listBox1.SelectedItem != null)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
            }
        }

        void Building(traBuilding build)
        {
            if (build is traBuilding)
            {
                traBrowser1.NavigateURL(build.URL);
                HtmlElement contract = traBrowser1.GetElementByID("contract");
                HtmlElementCollection a = contract.GetElementsByTagName("a");
                if (a == null || a.Count.Equals(0))
                {
                    return;
                }
                traBrowser1.NavigateURL(a[0].GetAttribute("href"));
            }
        }

        void Queue()
        {
            foreach (object obj in listBox1.Items)
            {
                if (obj is traBuilding)
                {
                    Building(obj as traBuilding);                    
                }                
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            Queue();
            timer1.Start();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            int iTime ;
            //預設為三分鐘
            iTime = string.IsNullOrEmpty(txtTime.Text) ? 3 : Convert.ToInt16(txtTime.Text);
            if (btnStart.Text.Equals("Start"))
            {
                btnStart.Text = "Stop";
                timer1.Interval = 10000 * iTime;
                timer1.Start();
            }
            else
            {
                timer1.Stop();
                btnStart.Text = "Start";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Queue();
        }
    }
}
